package com.isoftstone.industry.aaa.core.service.accounting.charging.impl;

import org.springframework.stereotype.Service;

import com.isoftstone.industry.persistance.bean.BillingCycle;
import com.isoftstone.industry.persistance.bean.BillingList;
import com.isoftstone.industry.persistance.bean.ChargingItem;
import com.isoftstone.industry.persistance.bean.ChargingItemDetail;
import com.isoftstone.industry.persistance.bean.PricingInfo;
import com.isoftstone.industry.persistance.bean.PricingInfoSnapshot;
import com.isoftstone.industry.persistance.bean.ProductInstance;
import com.isoftstone.industry.persistance.bean.TariffSnapshot;
import com.isoftstone.industry.persistance.bean.User;
import com.isoftstone.industry.aaa.core.log.ILogMessages;
import com.isoftstone.industry.aaa.core.service.accounting.charging.IChargingEngine;

@Service("nonRecurringChargingEngine")
public class NonRecurringChargingEngine extends AbstractChargingEnginer implements IChargingEngine
{
	public NonRecurringChargingEngine()
	{
		super();
	}

	public NonRecurringChargingEngine(User user, BillingCycle billingCycle, ChargingItemDetail chargingItemDetail)
	{
		super(user, billingCycle, chargingItemDetail);
	}

	@Override
	public void charge4Postpaid()
	{
		PricingInfoSnapshot pricingInfoSnap = chargingItemDetail.getPricingInfoSnapshot();
		ChargingItem chargingItem = chargingItemDetail.getChargingItem();
		ProductInstance prodInst = chargingItem.getProdInst();
		String calcMethod = pricingInfoSnap.getCalcMethod();
		// 一次性计费只支持直接计算
		if (PricingInfo.ICalcMethod.DIRECT.equals(calcMethod))
		{
			TariffSnapshot tariffSnap = pricingInfoSnap.getTariffList().get(0);
			// 一次性计费的资费信息的计算方式是直接计算
			Double amount = tariffSnap.getFixedPrice();
			logger.info(ILogMessages.CHARGING_NONRECURRING4USER, user.getUsername(), prodInst.getSn(), billingCycle.getName(), pricingInfoSnap
					.getName(), amount);
			BillingList billingList = createBillingList(user, chargingItemDetail, prodInst, pricingInfoSnap.getAcctItemType(),
					BillingList.Type.POST_PAID_NONRECURRING, amount, null, null, null, null);
			logger.info(ILogMessages.CHARGING_CREATE_BILLING_LIST, user.getUsername(), prodInst.getSn(), billingList.getSn(),
					BillingList.Type.POST_PAID_NONRECURRING, pricingInfoSnap.getAcctItemType().getName(), amount);
			// 一次性计费只需要计费一次，把该计费子项明细标识为无效状态
			chargingItemDetailService.markInvalid(chargingItemDetail.getId());
		} else
		{
			throw new UnsupportedOperationException("非法的计费模式，一次性计费只支持直接计算方法！");
		}

	}

	@Override
	public void charge4Prepaid()
	{
		BillingList billingList = null;
		PricingInfoSnapshot pricingInfoSnap = chargingItemDetail.getPricingInfoSnapshot();
		ChargingItem chargingItem = chargingItemDetail.getChargingItem();
		ProductInstance prodInst = chargingItem.getProdInst();
		String calcMethod = pricingInfoSnap.getCalcMethod();
		// 一次性计费只支持直接计算
		if (PricingInfo.ICalcMethod.DIRECT.equals(calcMethod))
		{
			TariffSnapshot tariffSnap = pricingInfoSnap.getTariffList().get(0);
			// 一次性计费的资费信息的计算方式是直接计算
			Double amount = tariffSnap.getFixedPrice();
			logger.info(ILogMessages.CHARGING_NONRECURRING4USER, user.getUsername(), prodInst.getSn(), pricingInfoSnap.getName(), amount);
			billingList = createBillingList(user, chargingItemDetail, prodInst, pricingInfoSnap.getAcctItemType(),
					BillingList.Type.PRE_PAID_NONRECURRING, amount, null, null, null, null);
			logger.info(ILogMessages.CHARGING_CREATE_BILLING_LIST, user.getUsername(), prodInst.getSn(), billingList.getSn(),
					BillingList.Type.PRE_PAID_NONRECURRING, pricingInfoSnap.getAcctItemType().getName(), amount);
			// 一次性计费只需要计费一次，把该计费子项明细标识为无效状态
			chargingItemDetailService.markInvalid(chargingItemDetail.getId());
		} else
		{
			throw new UnsupportedOperationException("非法的计费模式，一次性计费只支持直接计算方法！");
		}
	}

}
